package com.finace.money.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.finace.money.R;
import com.finace.money.common.util.DateUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.DealRecordListBean;
import com.finace.money.interfaces.OnItemClickListener;
import com.finace.money.ui.adapter.holder.FootViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhangpeng on 2017/11/23.
 */

public class HomeNewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int TYPE_IMAGE = 0;
    private final int TYPE_IMAGE_RIGHT = 1;
    private final int TYPE_NULL = 2;
    private final int TYPE_FOOTER = 6;

    private final int EMPTY = 0;
    //正在加载更多
    private final int LOADING_MORE = 1;
    //没有更多
    private final int NO_MORE = 2;
    //脚布局当前的状态,默认为没有更多
    private int footer_state = 0;
    private List<DealRecordListBean.RowsBean> mDatas;
    private OnItemClickListener mListener;

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }

    public void setmDatas(List<DealRecordListBean.RowsBean> mDatas) {
        this.mDatas = mDatas;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else if (null != mDatas && mDatas.size() > 0) {
            DealRecordListBean.RowsBean rowsBean = mDatas.get(position);
            if (null != rowsBean) {
                if (rowsBean.getShow_model() == 1) {//纯简介
                    return TYPE_NULL;
                } else if (rowsBean.getShow_model() == 2) {//图片+简介
                    return TYPE_IMAGE_RIGHT;
                } else if (rowsBean.getShow_model() == 3) {//只显示图片
                    return TYPE_IMAGE;
                } else {
                    return TYPE_NULL;
                }
            } else {
                return TYPE_NULL;
            }
        } else {
            return TYPE_NULL;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            return new FootViewHolder(View.inflate(UIUtil.getContext(),
                    R.layout.item_recycler, null));
        } else if (viewType == TYPE_IMAGE) {
            return new HomeNewsViewHolder(LayoutInflater.from(UIUtil.getContext()).inflate(
                    R.layout.item_home_news_image, parent, false));
        } else if (viewType == TYPE_IMAGE_RIGHT) {
            return new HomeNewsViewHolder(LayoutInflater.from(UIUtil.getContext()).inflate(
                    R.layout.item_home_news_right, parent, false));
        } else if (viewType == TYPE_NULL) {
            return new HomeNewsViewHolder(LayoutInflater.from(UIUtil.getContext()).inflate(
                    R.layout.item_home_news_null, parent, false));
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            if (position == 0) {//如果第一个就是脚布局,,那就让他隐藏
                footViewHolder.mProgressBar.setVisibility(View.GONE);
                footViewHolder.tv_line1.setVisibility(View.GONE);
                footViewHolder.tv_line2.setVisibility(View.GONE);
                footViewHolder.tv_state.setText("");
            }
            switch (footer_state) {
                case EMPTY:
                    footViewHolder.itemView.setVisibility(View.GONE);
                    break;
                case LOADING_MORE:
                    footViewHolder.itemView.setVisibility(View.VISIBLE);
                    footViewHolder.mProgressBar.setVisibility(View.VISIBLE);
                    footViewHolder.tv_line1.setVisibility(View.GONE);
                    footViewHolder.tv_line2.setVisibility(View.GONE);
                    footViewHolder.tv_state.setText(UIUtil.getString(R.string.loading_now));
                    break;
                case NO_MORE:
                    footViewHolder.itemView.setVisibility(View.VISIBLE);
                    footViewHolder.mProgressBar.setVisibility(View.GONE);
                    footViewHolder.tv_line1.setVisibility(View.VISIBLE);
                    footViewHolder.tv_line2.setVisibility(View.VISIBLE);
                    footViewHolder.tv_state.setText(UIUtil.getString(R.string.data_empty));
                    break;
            }

        } else if (holder instanceof HomeNewsViewHolder) {
            HomeNewsViewHolder homeNewsViewHolder = (HomeNewsViewHolder) holder;

            if (null != mDatas && mDatas.size() > 0) {
                DealRecordListBean.RowsBean rowsBean = mDatas.get(position);
                if (null != rowsBean) {
                    homeNewsViewHolder.tvItemNewsTitle.setText(rowsBean.getTitle());
                    homeNewsViewHolder.tvItemNewsDate.setText(DateUtil.timeStampToString(Long.valueOf(rowsBean.getCreate_time()), DateUtil.DATE_TO_STRING_DETAIAL_PATTERN2));
                    Glide.with(UIUtil.getContext()).load(rowsBean.getPic()).into(homeNewsViewHolder.ivItemNewsIcon);
                }
            }
            homeNewsViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != mListener){
                        mListener.onItemClick(view,position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return (null != mDatas && mDatas.size() > 0) ? mDatas.size() + 1 : 0;
    }

    public class HomeNewsViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_item_news_title)
        TextView tvItemNewsTitle;
        @BindView(R.id.tv_item_news_date)
        TextView tvItemNewsDate;
        @BindView(R.id.iv_item_news_icon)
        ImageView ivItemNewsIcon;

        public HomeNewsViewHolder(View inflate) {
            super(inflate);
            ButterKnife.bind(this, inflate);
        }
    }

    public void changeState(int state) {
        this.footer_state = state;
        notifyDataSetChanged();
    }
}
